package team.archai.nexus.boot.platform.core.config;

import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.parameters.HeaderParameter;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.GroupedOpenApi;
import org.springdoc.core.customizers.GlobalOpenApiCustomizer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import team.archai.nexus.boot.auth.constant.AuthConstant;

/**
 * @author lishibin
 * @desc: 文档配置
 * @date 2023-05-19
 */


@Configuration
public class ApiDocConfig {

    @Bean
    public OpenAPI archaiNexusOpenApi() {
        return new OpenAPI()
                .info(new Info()
                        .title("Archai Nexus Nest ApiDoc")
                        .version("0.0.1-alpha")
                        .description("Archai Nexus Nest接口文档")
                        .termsOfService("https://example.com")
                        .license(new License().name("Apache 2.0")
                                .url("https://example.com")).contact(new Contact()
                                .name("archai-nexus-team")
                                .email("15249263150@sina.cn")
                                .url("https://example.com")
                        ))
                .components(new Components()
                        .addSecuritySchemes(AuthConstant.TOKEN_HEADER,
                                new SecurityScheme()
                                        .type(SecurityScheme.Type.APIKEY)
                                        .in(SecurityScheme.In.HEADER)
                                        .name("认证Token")
                                        .description("全局认证 Token")
                        ).addSecuritySchemes(AuthConstant.BASIC_HEADER_KEY,
                                new SecurityScheme()
                                        .type(SecurityScheme.Type.APIKEY)
                                        .in(SecurityScheme.In.HEADER)
                                        .name("签名值")
                                        .description("签名值,格式 \"Archai-Nexus-Nest $client_id:$Signature\" $client_id 为分配得客户端id $Signature为对应得密钥")
                        )
                )
                // .addSecurityItem(new SecurityRequirement().addList(AuthConstant.TOKEN_HEADER))
                ;
    }

    /**
     * 每个接口添加header
     * @return
     */
    @Bean
    public GlobalOpenApiCustomizer addlobalOpenApiCustomizer() {
        return openApi -> {
            // 全局添加鉴权参数
            if (openApi.getPaths() != null) {
                openApi.getPaths().forEach((s, pathItem) -> pathItem.readOperations().forEach(operation -> {
                    Parameter parameter = new Parameter();
                    parameter.setIn(ParameterIn.HEADER.toString());
                    parameter.setName(AuthConstant.TOKEN_HEADER);
                    parameter.setRequired(true);
                    parameter.setDescription("全局认证Token");
                    operation.addParametersItem(parameter);

                    Parameter authorization = new Parameter();
                    authorization.setIn(ParameterIn.HEADER.toString());
                    authorization.setName(AuthConstant.BASIC_HEADER_KEY);
                    authorization.setRequired(true);
                    authorization.setDescription("签名值,格式 \"Archai-Nexus-Nest $client_id:$Signature\" $client_id 为分配得客户端id $Signature为对应得密钥");
                    operation.addParametersItem(authorization);
                }));
            }

        };
    }

    @Bean
    @ConditionalOnProperty(name = "archai-nexus.basics-doc-enabled", havingValue = "true")
    public GroupedOpenApi platFormApi() {
        String[] paths = {"/**"};
        String[] packagedToMatch = {"team.archai.nexus.boot.platform"};
        return GroupedOpenApi.builder().group("平台基础系统模块")
                .pathsToMatch(paths)
                .addOperationCustomizer((operation, handlerMethod) -> operation.addParametersItem(new HeaderParameter().required(true).name(AuthConstant.TOKEN_HEADER).description("认证 Token")
                ))
                .packagesToScan(packagedToMatch).build();
    }

    @Bean
    public GroupedOpenApi signApi() {
        String[] paths = {"/**"};
        String[] packagedToMatch = {"null"};
        return GroupedOpenApi.builder().group("接口说明")
                .pathsToMatch(paths)
                .packagesToScan(packagedToMatch).build();

    }
}
